<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<ul>
			<li><a href="/">home</a></li>
			<li><a href="/about">about</a></li>
		</ul>
		<div class="routerView"></div>
		<script type="text/javascript">
			window.addEventListener("DOMContentLoaded",()=>{
				history.pushState(null,'',"/")
				RouterRender(location.pathname)
				// 获取页面所有的a标签
				var linklist = document.querySelectorAll("a[href]");
				linklist.forEach((item=>{
					item.addEventListener('click',(e)=>{
						// 取消a标签的默认事件（页面跳转）
						e.preventDefault()
						console.log(123)
						// history.pushState,可以修改我们的路径而不会触发页面的跳转
						history.pushState(null,'',item.getAttribute('href'))
						RouterRender(item.getAttribute('href'))
					})
				}))
			})
			
			let routerView = [
				{
					path:"/",
					content:"<div>这是首页</div>"
				},{
					path:"/about",
					content:"<div>这是ABOUT</div>"
				}	
			
			]
			function RouterRender(url){
				let resContent = "<h1>404 not fond</h1>";
				routerView.forEach((item,i)=>{
					if(url==item.path){
						resContent = item.content;
					}
				})
				let divRouterview =document.querySelector('.routerView')
				divRouterview.innerHTML = resContent;
			}
			
		</script>
	</body>
</html>
